package com.clkj.sunhelp.mvp.publicityshow.publicityofshenpiprocess;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForShenPiStepPublicity;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.PublicityOfProcessStepResultItem;
import com.clkj.sunhelp.common.Constant;
import com.clkj.sunhelp.common.IntentKey;
import com.clkj.sunhelp.mvp.base.BaseActivity;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**审批界面公示展示界面
 * Created by Administrator on 2017/3/31.
 */

public class ActivityPublicityOfShenPiProcessShowView extends BaseActivity implements PublicityOfShenPiProcessShowContract.View,PullToRefreshBase.OnRefreshListener2{

    private PullToRefreshListView lv;
    private ListView originalLv;
    private RelativeLayout layoutEmpty;
    private TextView tvEmptytip;


    List<PublicityOfProcessStepResultItem> mPublicityOfShenPiStepInfoList = new ArrayList<>();
    ListAdapterForShenPiStepPublicity mAdapter;


    Integer mPageIndex;
    boolean canLoad;
    boolean isRefresh;


    String mRealName;
    String mIdCardNo;

    PublicityOfShenPiProcessShowContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publicity_of_shenpi_process_show_view);
        initTitleWithRightTvOrIv(null,null,"审批节点公示查询结果",true,null,null);
        initData();
        assignView();
        initView();

        mPresenter = new PublicityOfShenPiProcessShowPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

        getOriginalList();

    }

    @Override
    public void initData() {
        mRealName = getIntent().getStringExtra(IntentKey.INTENT_KEY_PERSONNAME);
        mIdCardNo = getIntent().getStringExtra(IntentKey.INTENT_KEY_IDCARD_NO);
    }

    @Override
    public void assignView() {
        lv = (PullToRefreshListView) findViewById(R.id.lv);
        originalLv = lv.getRefreshableView();

        layoutEmpty = (RelativeLayout) findViewById(R.id.layout_empty);
        tvEmptytip = (TextView) findViewById(R.id.tv_emptytip);
    }

    @Override
    public void initView() {
        lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lv.setOnRefreshListener(this);

        mAdapter = new ListAdapterForShenPiStepPublicity(this,mPublicityOfShenPiStepInfoList);
        originalLv.setAdapter(mAdapter);

    }


    private void getOriginalList()
    {
        isRefresh = true;
        mPageIndex = 1;
        getShenPiProcessInfo();
    }


    private void getMoreList()
    {
        if (canLoad)
        {
            mPageIndex ++;

            getShenPiProcessInfo();

        }

    }

    @Override
    public void getShenPiProcessInfo() {
        mPresenter.getShenPiProcessInfo(
                TextUtils.isEmpty(mRealName)==false?mRealName:"",
                TextUtils.isEmpty(mIdCardNo)==false?mIdCardNo:"",
                mPageIndex.toString(),
                Constant.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void onGetShenPiProcessInfoSuccess(List<PublicityOfProcessStepResultItem> publicityList) {
        lv.onRefreshComplete();

        if (isRefresh)
        {
           mPublicityOfShenPiStepInfoList.clear();
           isRefresh = false;
        }
        if (publicityList.size()>0)
        {
           if (publicityList.size()>=Constant.DEFAULT_PAGE_SIZE)
           {
              lv.setMode(PullToRefreshBase.Mode.BOTH);
              canLoad = true;
           }else{
              canLoad = false;
              lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
           }
            mPublicityOfShenPiStepInfoList.addAll(publicityList);

        }else{
              canLoad = false;
              lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

              if (mPublicityOfShenPiStepInfoList.size()==0)
              {
                lv.setEmptyView(layoutEmpty);
              }else{
                ToastUtil.show(this, "已经获取全部数据", 2000);
                    }
              }

              mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetShenPiProcessInfoError(String tip) {
        lv.onRefreshComplete();
        ToastUtil.showShort(this, tip);
        if (isRefresh) {
            mPublicityOfShenPiStepInfoList.clear();
            lv.setEmptyView(layoutEmpty);

        }

        isRefresh = false;
        canLoad = false;

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(PublicityOfShenPiProcessShowContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false,null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        getOriginalList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        getMoreList();
    }
}
